package com.cqjtu.store.product.controller;

import com.cqjtu.store.common.*;
import com.cqjtu.store.product.pojo.dto.ProductCategoryPageDTO;
import com.cqjtu.store.product.pojo.dto.ProductSettlementDTO;
import com.cqjtu.store.product.pojo.entity.Product;
import com.cqjtu.store.product.pojo.vo.*;
import com.cqjtu.store.product.service.ProductService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/product")
@Slf4j
@Api(tags = "商品模块")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 新到好货
     * @return
     */
    @GetMapping("new")
    public R<List<ProductVO>> getNewList(){
        List<ProductVO> newList = productService.getNewList();
        return R.ok(newList);
    }

    /**
     * 热销排行
     * @return
     */
    @GetMapping("hot")
    @ApiOperation("热销排行")
    public R<List<ProductVO>> getHotList(){
        List<ProductVO> hotList = productService.getHotList();
        return R.ok(hotList);
    }

    @GetMapping("categoryList")
    @ApiOperation("查询商品分类列表")
    public R<List<ProductCategoryVO>> getPCategoryList(Integer parent, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        List<ProductCategoryVO> categoryVO = productService.getProductCategoryList(parent);
        return R.ok(categoryVO);
    }

    @GetMapping("shoppingCartList")
//    @ApiOperation("查询购物车列表")
    public R<List<ShoppingCartVO>> getShoppingCartList(HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        List<ShoppingCartVO> shoppingCartVOList = productService.getShoppingCartList(userLoginVO);
        return R.ok(shoppingCartVOList);
    }


//    @GetMapping("list")
//    public R<List<ProductListVO>> getListByCategoryId(@RequestParam(value = "typeid") Integer categoryId,HttpServletRequest request){
//        //获取用户的身份
//        String token = request.getHeader("Authorization");
//        UserLoginVO userLoginVO = null;
//        if(token != null || token != ""){
//            //已经登录
//            userLoginVO = JwtUtils.parseToken(token);
//        }
//        List<ProductListVO> productVOList = productService.getListByCategoryId(categoryId,userLoginVO);
//        return R.ok(productVOList);
//    }

    //分页功能
    @GetMapping("list")
    public R<PageVO<List<ProductListVO>>> getListByCategoryId(@RequestBody @Validated ProductCategoryPageDTO categoryPageDTO, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = null;
        if(token != null || token != ""){
            //已经登录
            userLoginVO = JwtUtils.parseToken(token);
        }
        PageVO<List<ProductListVO>> pageVO = productService.getListByCategoryId(categoryPageDTO,userLoginVO);
        return R.ok(pageVO);
    }

    @GetMapping("list2")
    public R<PageInfo<Product>> getListByCategoryId2(@RequestBody ProductCategoryPageDTO categoryPageDTO){
        PageInfo<Product> productVOList = productService.getListByCategoryId(categoryPageDTO);
        return R.ok(productVOList);
    }

    @GetMapping("list3")
    public R<PageVO<List<ProductVO>>> getListByCategoryId3(@RequestBody ProductCategoryPageDTO categoryPageDTO){
//        PageInfo<Product> productVOList = productService.getListByCategoryId(categoryPageDTO);
        PageVO<List<ProductVO>> listPageVO = productService.getListByCategoryId3(categoryPageDTO);
        return R.ok(listPageVO);
    }

    /**
     * 查询商品详情
     * @param id
     * @param request
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("查询商品详情")
    public R<ProductDetailsVO> getById(@PathVariable Integer id,HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");

        UserLoginVO userLoginVO = null;
        if(token != null || token != ""){
            //已经登录
            userLoginVO = JwtUtils.parseToken(token);
        }
        ProductDetailsVO detailsVO = productService.getProductDetailsById(id,userLoginVO);
        return R.ok(detailsVO);
    }



    @GetMapping("favorite")
    public R<PageVO<List<ProductVO>>> getFavoriteByUser(@RequestBody PageDTO pageDTO,HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        PageVO<List<ProductVO>> listPageVO = productService.getFavoriteListByUser(pageDTO,userLoginVO);
        return R.ok(listPageVO);
    }


    @PostMapping("settlement")
    public R<List<ShoppingCartVO>> settlement(@RequestBody ProductSettlementDTO[] productSettlementDTO,
                                                 HttpServletRequest request){
        //获取用户的身份
        System.out.println("oooooooooooooooooooooooooooooooooooooooooooo");
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        System.out.println(productSettlementDTO[0].getSettlementId()+"okok");
        List<ShoppingCartVO> listPageVO = productService.getSettlement(productSettlementDTO,userLoginVO);
        return R.ok(listPageVO);
    }

    /**
     * 新增商品-后台管理系统
     * @param product
     * @param request
     * @return
     */
    @PostMapping("add")
    public R<Void> addProduct(@RequestBody @Validated Product product ,
                               HttpServletRequest request){
        log.debug("新增商品信息:{}",product);
       //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productService.addProduct(product,userLoginVO);
        return R.ok();
    }

    /**
     * 修改商品信息-后台管理系统
     * @param product
     * @param request
     * @return
     */
    @PostMapping("update")
    public R<Void> updateAddress(@RequestBody @Validated Product product,HttpServletRequest request){
        log.debug("修改的商品信息；{}",product);
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productService.update(product,userLoginVO);
        return R.ok();
    }

    /**
     * 后台商品管理--商品查询
     */
    @GetMapping("productlist")
    public R<PageVO<List<ProductBackListVO>>> getProductList(@RequestBody @Validated PageDTO pageDTO){
        PageVO<List<ProductBackListVO>> pageVO = productService.getProductList(pageDTO);
        return R.ok(pageVO);
    }

    /**
     * 后台商品管理--商品删除
     */
    @DeleteMapping("delete/{id}")
    public R<Void> deleteProduct(@PathVariable Integer id){
        productService.deleteProduct(id);
        return R.ok();
    }

    /**
     * 后台商品管理--商品上架
     */
    @PutMapping("grounding/{id}")
    public R<Void> grounding(@PathVariable Integer id){
        productService.grounding(id);
        return R.ok();
    }

    /**
     * 后台商品管理--商品下架
     */
    @PutMapping("off/{id}")
    public R<Void> offShelf(@PathVariable Integer id){
        productService.offShelf(id);
        return R.ok();
    }





}
